;##########################################
; Variable Width Font routine.            #
; Compiled with Byuu's XKAS Compiler      #
;##########################################

!VRAM_XPOS  = $1134     ; VRAM Tile Position
!TEMP_XPOS  = $114E     ; TEMP Tile Position 
!PIXELS     = $10A0     ; PixelsLeft on Tile
!VRAM_LINE  = $1152     ; Current VRAM Line
!CHAR       = $10A2     ; the last character read

!BUF_T      = $10B4     ; the char data we use that has font
!BUF_TR     = $10BC     ; the char data we use that has font

!BUF_B      = $10C4     ; the char data we use that has font
!BUF_BR     = $10CC     ; the char data we use that has font


!BUF_2T     = $10D4     ; the char data we use that has font
!BUF_2TR    = $10DC     ; the char data we use that has font

!BUF_2B     = $10E4     ; the char data we use that has font
!BUF_2BR    = $10CC     ; the char data we use that has font


!BUF_MASK   = $10F4     ; Masking buffer, gets altered data
!T_WIDTH    = #$08      ; Width of a TILE  
!AND_VAL    = #$07


lorom


;-------------------------;
;                         ;
;                         ;
;-------------------------;
main() {
  ;--------------------------------;
  ; VERY IMPORTANT!!!!!!!!!!!!!!!! ;
  ;--------------------------------;
  org $80EC71
  NOP
  NOP
  ;--------------------------------;
  org $80EE1F
  db $20


  ;--------------------------------;
  ;                                ;
  ;--------------------------------;
  org $80F43E
  JSR MyVWF
  PLP                   ; Kills Shadow
  RTS  
;  LDA $1105,X            ; Enables Shadow

  org $80EC89
  db $EA,$EA,$EA,$EA,$EA,$EA 

  org $80EE4B     ; This makes $114E NOT increment
  NOP

  org $80EE52     ; This makes $1134 NOT increment twice
  NOP
  NOP
  NOP

  org $80EE58     ;
  NOP
  NOP

  org $80EE5A     ; This makes $1134 NOT increment twice
  JSR Do_Tile_INC

;--------------------------------;
;                                ;
;--------------------------------;
  org $80F800
MyVWF:
;############################
; Wait for vertical blank.  #
; This was added because the#
; the font was screwing up  #
; due to timing issues!     #
;############################
  PHA
  PHP
  SEP #$20

Vloop:
  LDA $4210
  AND #$80
  BEQ Vloop
  PLP
  PLA
;############################
;                           #
;############################

  LDA !PIXELS                ; If his contains ANYTHING but zero
  BNE DoVwf                  ; then we have VWF Tile space left to use

;--------------------;
;                    ;
;--------------------;
NoVwf:
  PHP
  REP #$20
  LDA !CHAR
  AND #$00FF
  TAX

  SEP #$20
  LDA WIDTHS,X
;  %data $BF $00 $FA $80       ; LDA WidthTBL,X
  PHA
  LDA !T_WIDTH
  SEC 
  SBC $01,S
  AND !AND_VAL
  STA !PIXELS

;----------------------------;
; Transfer font to MY buffer ;
;----------------------------;
  LDX #$0000
Trans:
  LDA !BUF_T,X
  STA $7FFC00,X
  INX
  CPX #$0020
  BNE Trans
 
  PLA                ; Fonts width. garbage    
  PLP
  LDX #$0006
RTS


;--------------------;
;                    ;
;                    ;
;                    ;
;--------------------;
DoVwf:
  PHP
  SEP #$20

;-----------------;
;                 ;
;-----------------;
_IsGood:
  LDY #$0000
  LDA !PIXELS             ; Pixels left
  PHA                    ; save it

;-----------------;
;                 ;
;-----------------;
_Looper:
  REP #$20  
  LDA $01,S
  AND #$00FF
  DEC
  ASL
  TAX

    LDA !BUF_T,Y     ; Adjustments made in Sub call
    JSR DoShift
    ;----------
    SEP #$20
    PHX
    TYX

    STA $7FFC08,X   ; Save the lost BITS
    STA !BUF_TR,X
    XBA             ; The VWF Tile Data. WHat we shifted to adjust
                    ; to fit on the current WORKING TILE 
    ORA $7FFC00,X   ; My Buffer
    STA $7FFC00,X
    STA !BUF_T,X
   
    ;-----------;
    ;           ;
    ;-----------;
    PLX
    REP #$20  
    LDA !BUF_B,Y    ; Adjustments made in Sub call
    JSR DoShift
    ;----------
    SEP #$20
    TYX

    STA $7FFC18,X  ; Save the lost BITS   
    STA !BUF_BR,X
    XBA            ; The VWF Tile Data

    ORA $7FFC10,X  ; My Buffer 
    STA $7FFC10,X
    STA !BUF_B,X

    INY             ; Next BYTE to shift 
    CPY #$0008
  BNE _Looper

;-----------------------------;
; Transfer data to MASK buffer;
;-----------------------------;
  LDX #$001F
DoMask:
  LDA !BUF_T,X
  STA !BUF_MASK,X
  DEX
 BPL DoMask


;------------------------;
;                        ;
;------------------------;
  LDA #$00
  XBA  
  LDA !CHAR
  TAX
  LDA WIDTHS,X
;  %data $BF $00 $FA $80       ; LDA WidthTBL,X
  PHA
  LDA !PIXELS
  SEC
  SBC $01,S
  BPL _GotPixels

  ;-----------------;
  ; Fix overflow    ;
  ;-----------------;
  EOR #$FF
  INC
  STA !CHAR
  LDA !T_WIDTH
  SEC
  SBC !CHAR

  PHA                     ; Save New pixels  
  ;------------------------;
  ; Transfer the SAVE BITS ;
  ; to start of MY TILE    ;
  ;------------------------;
  LDX #$0000
_Saved:
  LDA $7FFC08,X
  STA $7FFC00,X
  LDA #$00
  STA $7FFC08,X
;----------------
  LDA $7FFC18,X
  STA $7FFC10,X
  LDA #$00
  STA $7FFC18,X

  INX
  CPX #$0008
  BNE _Saved
  
  PLA                     ; Get New pixels back
  AND !AND_VAL
  ORA #$80
  STA !PIXELS

  PLA            ; 2nd STACKED Bits left, garbage now
  PLA            ; 1st STACKED Bits left, garbage now
  LDX #$0006
  PLP
RTS  


;-----------------------------;
; End of VWF Routine          ;
;-----------------------------;  
_GotPixels:
  AND !AND_VAL
  STA !PIXELS

  PLA            ; 2nd STACKED Bits left, garbage now
  PLA            ; 1st STACKED Bits left, garbage now
  LDX #$0006
  PLP
RTS  


;-------------------;
; Shifting Code     ;
;-------------------;
DoShift:
  AND #$00FF
  XBA
  JMP (_Shifter,X)  

_Shifter:
 dw _1,_2,_3,_4,_5,_6,_7

;-------------;
;             ;
;-------------;
_1:
  LSR
_2:
  LSR
_3:
  LSR
_4:
  LSR
_5:
  LSR
_6:
  LSR
_7:
  LSR

RTS



;----------------------------;
; I control TILE incrememnts ;
;----------------------------;
Do_Tile_INC:
  PHP
  SEP #$20
  LDA !PIXELS   
  BPL _NoOVERFLOW
  
  AND #$7F           ; If a VWF Tile overflow
  STA !PIXELS
  INC !VRAM_XPOS
  BRA _PIX_LEFT    

;------------------;
;                  ;
;------------------;
_NoOVERFLOW:
  BNE _PIX_LEFT
 
  INC !VRAM_XPOS

_PIX_LEFT:
  PLP
RTS

;--------------------;
; Width Table        ;
;--------------------;
  org $80FA00
WIDTHS:
  incbin C:\Romhack\trans\LFantasy2\bin\LFAN2FAT.WID
}



; $80/Ec06 -> STA $10A2
; $80/F800 -> My Routine
; $80/ED1F -> Trans to TEMP buffer